package service;

import java.sql.Connection;
import java.sql.SQLException;

public abstract class AbstractConnection {
	protected Connection conn=null;
	public Connection dataConnection() {
		String userName = "root";
		String password = "123456";
		String url = "jdbc:mysql://localhost/test?"
				+ "user=root&password=123456";
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("khong load duoc thu vien");
			e.printStackTrace();
		}
		try {
			conn = java.sql.DriverManager
					.getConnection(url, userName, password);
		} catch (SQLException e) {
			System.out.println("Khong ket noi duoc db");
			e.printStackTrace();

		}
		return conn;
	}

	public void closeConnection(){
		try {
			if(conn != null || !conn.isClosed())
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public Connection getCurrentConnection(){
		try {
			if(conn == null || conn.isClosed()) {
				conn = dataConnection();
			} 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return conn;
	}
}
